package lesson1_20.lesson6;


public class kebiancanshu {
    public static void main(String[] args) {
        int sum;
        sum = add(1, 2, 3, 4, 5, 6);
        System.out.println(sum);


        sum = add(new int[]{1, 2, 3, 4});
        System.out.println(sum);

        sum = add();
        System.out.println(sum);

        String msg;
        msg = add2("OK", 1, 2, 3, 4);
        System.out.println(msg);
    }

    public static int add(int... data) {
        int sum = 0;
        for (int x = 0; x < data.length; x++) {
            sum += data[x];
        }
        return sum;
    }

    //如果有其他参数，可变参数需要写在最后
    //一个方法只能设置一个可变参数
    public static String add2(String msg, int... data) {
        int sum = 0;
        for (int x = 0; x < data.length; x++) {
            sum += data[x];
        }
        return "加法运行结果：" + sum + ",消息:" + msg;
    }
}